package com.sx.baseproject.framework.app;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.mvvm.base.BaseActivity;
import com.example.mvvm.base.BaseFragment;
import com.example.mvvm.rx.RxBus;
import com.sx.widget.app.dialog.BaseDialog;
import com.sx.widget.app.dialog.WaitDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

public abstract class MyFragment<D extends ViewDataBinding, A extends BaseActivity> extends BaseFragment<D,A> {
    private List<Disposable> rxBusManager;
    private View view;
    private PublishSubject<Boolean> onResumeObservable;
    private Disposable observable;

    @Override
    public void initParam() {
        super.initParam();
        ARouter.getInstance().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isShowDialog()) {
            hideDialog();
        }
        if (rxBusManager != null) {
            for (Disposable disposable : rxBusManager) {
                if (disposable != null && !disposable.isDisposed()) {
                    disposable.dispose();
                }
            }
            rxBusManager = null;
        }
    }



    /**
     * 加载对话框
     */
    private BaseDialog mDialog;
    /**
     * 对话框数量
     */
    private int mDialogTotal;

    /**
     * 当前加载对话框是否在显示中
     */
    public boolean isShowDialog() {
        return mDialog != null && mDialog.isShowing();
    }

    /**
     * 显示加载对话框
     */

    @Override
    public void showDialog(String tv) {
        mDialogTotal++;
        handler().postDelayed(() -> {
            if (mDialogTotal > 0 &&getMyActivity()!=null&& !getMyActivity().isFinishing()) {
                if (mDialog == null) {
                    mDialog = new WaitDialog.Builder(getContext())
                            .setMessage(tv)
                            .create();
                }
                if (!mDialog.isShowing()) {
                    mDialog.show();
                }
            }
        }, 300);
    }

    @Override
    public void showDialog(String info, boolean touch) {
        mDialogTotal++;
        handler().postDelayed(() -> {
            if (mDialogTotal > 0 && getMyActivity() != null && !getMyActivity().isFinishing()) {
                if (mDialog == null) {
                    mDialog = new WaitDialog.Builder(getContext())
                            .setMessage(info)
                            .create();
                }
                if (!mDialog.isShowing()) {
                    mDialog.setCanceledOnTouchOutside(!touch);
                    mDialog.show();
                }
            }
        }, 300);
    }

    /**
     * 隐藏加载对话框
     */
    @Override
    public void hideDialog() {
        if (mDialogTotal > 0) {
            mDialogTotal--;
        }

        if (mDialogTotal == 0 && mDialog != null && mDialog.isShowing() &&getMyActivity()!=null&& !getMyActivity().isFinishing()) {
            mDialog.dismiss();
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (onResumeUi() && onResumeObservable == null) {
                onResumeObservable = PublishSubject.create();
                observable = onResumeObservable.throttleFirst(300, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean show) throws Exception {
                                if (show) {
                                    onResumeUi();
                                }
                            }
                        });
            }
            if (onResumeUi() && onResumeObservable != null) {
                onResumeObservable.onNext(!hidden);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (observable != null && !observable.isDisposed()) {
            observable.dispose();
        }
    }

    /**
     * 这里是页面根据生命周期来刷新数据，做了单点的操作
     */
    public boolean onResumeUi() {
        return false;
    }

    @Override
    public Class getLoginActivity() {
        return null;
    }
}